{% from "../../macros/attributes.njk" import govukAttributes %}

{%- macro _actionLink(action, cardTitle) %}
  <a class="govuk-link {%- if action.classes %} {{ action.classes }}{% endif %}" href="{{ action.href }}"
    {{- govukAttributes(action.attributes) }}>
    {{- action.html | safe | indent(4) if action.html else action.text -}}
    {%- if action.visuallyHiddenText or cardTitle -%}
    <span class="govuk-visually-hidden">
      {%- if action.visuallyHiddenText %} {{ action.visuallyHiddenText }}{% endif -%}
      {%- if cardTitle %} ({{ cardTitle.html | indent(6) | safe if cardTitle.html else cardTitle.text }}){% endif -%}
    </span>
    {%- endif -%}
  </a>
{% endmacro -%}

{%- macro _summaryCard(params) %}
{%- set headingLevel = params.title.headingLevel if params.title.headingLevel else 2 -%}

<div class="govuk-summary-card {%- if params.classes %} {{ params.classes }}{% endif %}"
  {{- govukAttributes(params.attributes) }}>
  <div class="govuk-summary-card__title-wrapper">
  {% if params.title %}
    <h{{ headingLevel }} class="govuk-summary-card__title {%- if params.title.classes %} {{ params.title.classes }}{% endif %}">
      {{ params.title.html | safe | trim | indent(6) if params.title.html else params.title.text }}
    </h{{ headingLevel }}>
  {% endif %}
  {% if params.actions.items.length %}
    {% if params.actions.items.length == 1 %}
    <div class="govuk-summary-card__actions {%- if params.actions.classes %} {{ params.actions.classes }}{% endif %}">
      {{ _actionLink(params.actions.items[0], params.title) | trim | indent(4) }}
    </div>
    {% else %}
    <ul class="govuk-summary-card__actions {%- if params.actions.classes %} {{ params.actions.classes }}{% endif %}">
      {% for action in params.actions.items %}
      <li class="govuk-summary-card__action">
        {{ _actionLink(action, params.title) | trim | indent(8) }}
      </li>
      {% endfor %}
    </ul>
    {% endif %}
  {% endif %}
  </div>

  <div class="govuk-summary-card__content">
    {{ caller() | trim }}
  </div>
</div>
{% endmacro -%}

{#- Determine if we need 2 or 3 columns #}
{%- set anyRowHasActions = false %}
{% for row in params.rows %}
  {% set anyRowHasActions = true if row.actions.items | length else anyRowHasActions %}
{% endfor -%}

{%- set summaryList -%}
<dl class="govuk-summary-list {%- if params.classes %} {{ params.classes }}{% endif %}" {{- govukAttributes(params.attributes) }}>
{% for row in params.rows %}
  {% if row %}
  <div class="govuk-summary-list__row {%- if anyRowHasActions and not row.actions.items %} govuk-summary-list__row--no-actions{% endif %} {%- if row.classes %} {{ row.classes }}{% endif %}">
    <dt class="govuk-summary-list__key {%- if row.key.classes %} {{ row.key.classes }}{% endif %}">
      {{ row.key.html | safe | trim | indent(6) if row.key.html else row.key.text }}
    </dt>
    <dd class="govuk-summary-list__value {%- if row.value.classes %} {{ row.value.classes }}{% endif %}">
      {{ row.value.html | safe | trim | indent(6) if row.value.html else row.value.text }}
    </dd>
    {% if row.actions.items.length %}
    <dd class="govuk-summary-list__actions {%- if row.actions.classes %} {{ row.actions.classes }}{% endif %}">
      {% if row.actions.items.length == 1 %}
        {{- _actionLink(row.actions.items[0], params.card.title) | trim | indent(6, true) }}
      {% else %}
      <ul class="govuk-summary-list__actions-list">
        {% for action in row.actions.items %}
        <li class="govuk-summary-list__actions-list-item">
          {{ _actionLink(action, params.card.title) | trim | indent(8) }}
        </li>
        {% endfor %}
      </ul>
      {% endif %}
    </dd>
    {% endif %}
  </div>
  {% endif %}
{% endfor %}
</dl>
{%- endset %}

{%- if params.card -%}
  {% call _summaryCard(params.card) -%}
    {{ summaryList | safe | trim | indent(4) }}
  {%- endcall %}
{%- else -%}
  {{ summaryList | safe | trim }}
{%- endif %}
